package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Order;
import com.woniu.service.IOrderService;
import com.woniu.vo.ChargeReturnVo;
import com.woniu.vo.OrderSeachByIdReturnVo;
import com.woniu.vo.OrderSeachReturnVo;
import com.woniu.vo.OrderSearchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author long
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单管理")
public class OrderController extends BaseController{

    @Autowired
    private IOrderService iOrderService;

    //订单分页查询,根据车牌号，订单编号，时间区间联合分页查询
    @ApiOperation("订单分页查询")
    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result search (@PathVariable Integer currentPage,
                          @PathVariable Integer pageSize,
                          @RequestBody OrderSearchVo searchVo){
        PageResult page = iOrderService.search(currentPage,pageSize,searchVo);
        return toDataResult(page,"查询成功！");
    }

    //订单详情页查询,根据订单id查询详情
    @ApiOperation("订单详情页")
    @GetMapping("/{orderId}")
    public Result searchById(@PathVariable long orderId) throws ParseException {
        OrderSeachByIdReturnVo returnVo = iOrderService.searchById(orderId);
        System.out.println(returnVo.getCreateTime().getTime());
        return toDataResult(returnVo,"查询成功！");
    }

    //查询订单的收费规则，一个订单四行
    @ApiOperation("查询某订单收费规则")
    @GetMapping("/chargeRule/{roadId}")
    public Result searchChargeRule(@PathVariable long roadId){
        List<ChargeReturnVo> chargeReturnVoList = iOrderService.searchChargeRule(roadId);
        return toDataResult(chargeReturnVoList,"查询成功！");
    }

    //车辆驶入，拍照上传车牌号，根据车牌在车辆表里查
    //有该车就用车辆id创建订单
    //没有查到就新增车辆，返回id创建订单
    @ApiOperation("根据车牌号添加订单")
    @PostMapping("/{carNum}")
    public Result saveOrder(@PathVariable String carNum){
        //根据车牌生成订单
        boolean flag = iOrderService.saveOrder(carNum);
        return toResult(flag);
    }

    //根据id逻辑删除,结束订单
    @ApiOperation("根据id逻辑删除")
    @DeleteMapping("/{orderId}")
    public Result deleteOrderById(@PathVariable long orderId){
        //根据id逻辑删除
        boolean flag = iOrderService.removeById(orderId);
        return toResult(flag);
    }

    //根据订单id修改订单
    @ApiOperation("根据id修改订单")
    @PutMapping
    public Result updateOrderById(@RequestBody Order order){
        boolean flag = iOrderService.updateById(order);
        return toResult(flag);
    }

    //导出订单excel
    @ApiOperation("下载订单excel")
    @GetMapping("/excel")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("订单表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        //查询订单列表
        PageResult result = iOrderService.search(1, 10000, new OrderSearchVo());
        List<OrderSeachReturnVo> data = (List<OrderSeachReturnVo>)result.getData();
        EasyExcel.write(response.getOutputStream(), OrderSeachReturnVo.class).sheet("订单列表").doWrite(data);
    }

    //结算订单方法
    @ApiOperation("根据订单id结算")
    @GetMapping("/settle/{orderId}")
    public Result  settleOrder(@PathVariable long orderId){
        Order order = iOrderService.settleOrder(orderId);
        return toDataResult(order);
    }

}
